/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Derek
 */
public class SearchServlet extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    String[] cities={"Vancouver","Burnaby"};
    String[] countries={"Canada"};
    protected void searchUser(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Search For Users</title>");  
            CSSGenerator.generateSearchServletInlineCSS(out);
            out.println("</head>");
            out.println("<body>");
            
            out.println(
            "<center>"+
            "<table border>" +
            " <tr>" +
            "    <td align=\"right\" valign=\"top\"><p  class=\"searchLabels\">Search for</p></td>"  +       
            "    <td align=\"left\" valign=\"middle\">" );
            generateJumpMenu(out,request.getParameter("type"));
            
            out.println(
            "    </td>" +       
            " </tr>"         
                    
                    );
           //use if to control the search content page        
            
            // name and gender
            out.println(
            "<tr>" +
            "   <td>" +
            "       <p class=\"searchLabels\">Name</p>" +
            "   </td>" +        
            "   <td>" +
            "       <p class=\"searchLabels\">Gender</p>" +
            "   </td>" +                    
            "</td>" +
            "<tr>" +
            "   <td>" +
            "       <input type=\"text\" name=\"userName\" size=\"25\" >" +
            "   </td>" +        
            "   <td>" +
            "       <select size=\"1\" name=\"searchTypeSelection\">" +
            "          <option value=\"male\">Male</option>"+
            "          <option value=\"female\">Female</option>"+
            "       </select>" +
            "   </td>" +                    
            "</td>"                    
                    );
            
            // city and country
            out.println(
            "<form name=\"userSearchForm\" method=\"POST\" action=\"SearchServlet\">" +
            "<tr>" +
            "   <td>" +
            "       <p  class=\"searchLabels\">City</p>" +
            "   </td>" +        
            "   <td>" +
            "       <p class=\"searchLabels\">Country</p>" +
            "   </td>" +                    
            "</td>" +
            "<tr>" +
            "   <td>" +
            "       <select size=\"1\" name=\"cities\">" 
            );
            for(int i=0;i<cities.length;i++)
            {
                out.println(
            
                "          <option value=\""+cities[i] +"\">"+cities[i]+"</option>"
            //"          <option value=\"female\">Female</option>"
                );
            }
            out.println(
            "       </select>"+
            "   </td>" + 
                    
            "   <td>" +
            "       <select size=\"1\" name=\"countries\">" );
           for(int i=0;i<countries.length;i++)
            {
                out.println(
            
                "          <option value=\""+countries[i] +"\">"+countries[i]+"</option>"
            //"          <option value=\"female\">Female</option>"
                );
            }
            out.println(
            "       </select>" +
            "   </td>" +                    
            "</td>"                    
                    );
            out.println(
            "<tr>" +
            "   <td colspan=\"2\">" +
            "       <p class=\"searchLabels\">Email Address</p>" +
            "   </td>" +                          
            "</td>");
            
            out.println(
            "<tr>" +
            "   <td colspan=\"2\">" +
            "       <input type=\"text\" name=\"userName\" size=\"40\" >" +
            "   </td>" +        
            "</tr>");
         /*           
            "   <td>" +
            "       <select size=\"1\" name=\"searchTypeSelection\">" +
            "          <option value=\"male\">Male</option>"+
            "          <option value=\"female\">Female</option>"+
            "       </select>" +
            "   </td>" +                    
            "</td>"                    
         */   
            
                    
            out.println(
            "<tr>" +
            "  <td align=\"middle\" colspan=\"2\">" +
            "    <input type=\"submit\" name=\"userSearch\" value=\"Search\">" +        
            "  </td>" +        
            "</tr>" +       
            "</from>"        
                    );
            
            
            out.println(
            "<tr>" +
            "  <td align=\"left\" colspan=\"2\">" +
            "   <p class=\"copyRight\"> ClassNet© 2007  </p>"  +
            "  </td>" +        
            "</tr>" +       
            "</from>"        
                    );
            
            out.println(
                    
            "</table>" +        
            "</center>"
                    );
            
            
            out.println("</body>");
            out.println("</html>");
            
        } finally { 
            out.close();
        }
    } 
    protected void searchGroups(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        //ClassNet c = ClassNet.INSTANCE;
        try {
             
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Search For Users</title>");  
           CSSGenerator.generateSearchServletInlineCSS(out);
            out.println("</head>");
            out.println("<body>");
            
            out.println(
            "<center>"+
            "<table>" +
            " <tr>" +
            "    <td align=\"right\"  valign=\"top\"><p class=\"searchLabels\">Search for</p></td>"  +       
            "    <td align=\"left\"  valign=\"middle\">");
            generateJumpMenu(out,request.getParameter("type"));
         
            out.println(
            "    </td>" +       
            " </tr>"         
                    
                    );
            
            
            // creator and group name
            out.println(
            "<form name=\"groupSearchForm\" method=\"POST\" action=\"SearchServlet\">" +        
            "<tr>" +
            "   <td>" +
            "       <p class=\"searchLabels\">Created by</p>" +
            "   </td>" +        
            "   <td>" +
            "       <p class=\"searchLabels\">Group Name</p>" +
            "   </td>" +                    
            "</td>" +
            "<tr>" +
            "   <td>" +
            "       <input type=\"text\" name=\"creator\" size=\"35\" >" +
            "   </td>" +        
            "   <td>" +
            "       <input type=\"text\" name=\"groupName\" size=\"35\" >" +
            "   </td>" +                    
            "</td>"                    
                    );
            
            
            
            
            out.println(
            "<tr>" +
            "  <td align=\"middle\" colspan=\"2\">" +
            "    <input type=\"submit\" name=\"groupSearch\"value=\"Search\">" +        
            "  </td>" +        
            "</tr>" +       
            "</from>"        
                    );
            
            
            
            out.println(
                    
            "</table>" +        
            "</center>"
                    );
            
            
            out.println("</body>");
            out.println("</html>");
        } finally { 
            out.close();
        }
    }
    protected void searchNewsGroups(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        //ClassNet c = ClassNet.INSTANCE;
        try {
             
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Search For Course News Groups</title>");  
            CSSGenerator.generateSearchServletInlineCSS(out);
            out.println("</head>");
            out.println("<body>");
            
            out.println(
            "<center>"+
            "<table>" +
            " <tr>" +
            "    <td align=\"right\"  valign=\"top\"><p class=\"searchLabels\">Search for</p></td>"  +       
            "    <td align=\"left\"  valign=\"middle\">");
            generateJumpMenu(out,request.getParameter("type"));
            out.println(
            "    </td>" +       
            " </tr>"         
                    
                    );
            
            
            // creator and group name
            out.println(
            "<form name=\"newsGroupSearchForm\" method=\"POST\" action=\"SearchServlet\">" +        
            "<tr>" +
            "   <td>" +
            "       <p class=\"searchLabels\">Course Subject (eg. CPSC)</p>" +
            "   </td>" +        
            "   <td>" +
            "       <p class=\"searchLabels\">Course code (eg. 410)</p>" +
            "   </td>" +                    
            "</td>" +
            "<tr>" +
            "   <td>" +
            "       <input type=\"text\" name=\"courseSubject\" size=\"35\" >" +
            "   </td>" +        
            "   <td>" +
            "       <input type=\"text\" name=\"courseCode\" size=\"35\" >" +
            "   </td>" +                    
            "</td>"                    
                    );
            
            
            // creator and group name
            out.println(
            "<tr>" +
            "   <td>" +
            "       <p class=\"searchLabels\">Course Year (eg. 2007)</p>" +
            "   </td>" +        
            "   <td>" +
            "       <p class=\"searchLabels\">Course Title (eg. Advanced Software Engineering)</p>" +
            "   </td>" +                    
            "</td>" +
            "<tr>" +
            "   <td>" +
            "       <input type=\"text\" name=\"courseYear\" size=\"35\" >" +
            "   </td>" +        
            "   <td>" +
            "       <input type=\"text\" name=\"courseTitle\" size=\"35\" >" +
            "   </td>" +                    
            "</td>"                    
                    );
            
            
            
            
            out.println(
            "<tr>" +
            "  <td align=\"middle\" colspan=\"2\">" +
            "    <input type=\"submit\" name=\"newsGroupSearch\" value=\"Search\">" +        
            "  </td>" +        
            "</tr>" +       
            "</from>"        
                    );
            
            out.println(
            "<tr>" +
            "  <td align=\"left\" colspan=\"2\">" +
            "   <p class=\"copyRight\"> ClassNet© 2007  </p>"  +
            "  </td>" +        
            "</tr>" +       
            "</from>"        
                    );
            
            out.println(
                    
            "</table>" +        
            "</center>"
                    );
            
            
            out.println("</body>");
            out.println("</html>");
        } finally { 
            out.close();
        }
    }
    private void generateJumpMenu(PrintWriter out,String jumpTo)
    {
        out.println(
        "<form name=\"jumpForm\">" +
        "<select name=\"jumpMenu\" onChange=\"location=document.jumpForm.jumpMenu.options[document.jumpForm.jumpMenu.selectedIndex].value;\" value=\"GO\">" +
        "     <option value=\"SearchServlet?type=Users\" ");
        
        if(jumpTo==null || jumpTo.compareToIgnoreCase("Users")==0)
            out.println(" SELECTED ");
        
        out.println(
        "    >Users</option>"
              );
        
        
        
        out.println(      
        "     <option value=\"SearchServlet?type=Groups\"");
        if(jumpTo!=null && jumpTo.compareToIgnoreCase("Groups")==0)
            out.println(" SELECTED ");
         out.println(  
        "        >Groups</option>");
                 
                 
        out.println(         
        "     <option value=\"SearchServlet?type=NewsGroups\"");
        if(jumpTo!=null && jumpTo.compareToIgnoreCase("NewsGroups")==0)
            out.println(" SELECTED ");  
        out.println( 
        "        >News Groups</option>"+                    
        "</select>" +
        "</form>"
            );
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        if(request.getParameter("type") == null ||request.getParameter("type").compareToIgnoreCase("Users")==0 )
            searchUser(request, response);
        if(request.getParameter("type").compareToIgnoreCase("Groups")==0 )
            searchGroups(request, response);
        if(request.getParameter("type").compareToIgnoreCase("NewsGroups")==0 )
            searchNewsGroups(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
         response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        //ClassNet c = ClassNet.INSTANCE;
        try {
             
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Search Result</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Search result  at " + request.getContextPath () + "</h1>");
            //request.get
            if(request.getParameter("newsGroupSearch")!=null)
                 out.println("<h1> This is a search for news group</h1>");
            if(request.getParameter("groupSearch")!=null)
                 out.println("<h1> This is a search for groups</h1>");
            if(request.getParameter("userSearch")!=null)
                 out.println("<h1> This is a search for users</h1>");
            out.println("</body>");
            out.println("</html>");
            
        } finally { 
            out.close();
        }
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
